﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SchemaExplorer;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using ZX.WebApp.DesignForm.T4Model;

namespace ZX.WebApp.cspModel
{
    /// <summary>
    ///CodeSmithAutoCode 的摘要说明
    /// </summary>
    public class CodeSmithAutoCode
    {
        public CodeSmithAutoCode()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 预览生成的表单
        /// </summary>
        public static string CodeSmithComplete(DataTable dt, string colnum, string FormName, string TableName, string FormType)
        {
            //T4调用例子
            //UI u = new UI(dt, FormName);
            //return u.TransformText();

            if (FormType == "Details")
            {
                //UIandDetail ud = new UIandDetail(dt, FormName, TableName);
                //return ud.TransformText();
                UIandFormatDetail ud = new UIandFormatDetail(dt, FormName, TableName);
                return ud.TransformText();
            }
            else
            {
                UIandTable ut = new UIandTable(dt, FormName);
                return ut.TransformText();
            }
            //string errors = string.Empty;
            //string templateName = string.Empty;
            //CodeTemplate codeTemplate = null;

            ////TableEnum模板
            //string TemplateName = "cspModel/autoUIandTable.cst";
            //if (FormType == "Details") { TemplateName = "cspModel/autoUIandDetailsTable.cst"; }

            //templateName = HttpContext.Current.Server.MapPath(TemplateName);
            //codeTemplate = CompileTemplate(templateName, ref errors);
            //DatabaseSchema dbSchema = DBSchema.CreateDatabaseSchema();


            //if (string.IsNullOrEmpty(errors))
            //{
            //    codeTemplate.SetProperty("SourceDatabase", dbSchema);
            //    codeTemplate.SetProperty("datable", dt);
            //    codeTemplate.SetProperty("ColNums", colnum);
            //    codeTemplate.SetProperty("FormName", FormName);
            //    codeTemplate.SetProperty("TableName", TableName);
            //    errors = codeTemplate.RenderToString();
            //}

            //return errors;
        }
        /// <summary>
        /// 编译CodeSmith模板
        /// </summary>
        /// <param name="templateName"></param>
        /// <returns></returns>
        //public static CodeTemplate CompileTemplate(string templateName, ref string errors)
        //{
        //    CodeTemplate codeTemplate = null;
           
        //    //编译器
        //    CodeSmith.Engine.CodeTemplateCompiler compiler = new CodeSmith.Engine.CodeTemplateCompiler(templateName);
        //    compiler.Compile();

        //    //判断是否编译成功
        //    if (compiler.Errors.Count == 0)
        //    {
        //        codeTemplate = compiler.CreateInstance();
        //    }
        //    else
        //    {
        //        StringBuilder sbErrors = new StringBuilder();
        //        for (int i = 0; i < compiler.Errors.Count; i++)
        //        {
        //            sbErrors.Append(compiler.Errors[i].ToString());
        //        }
        //        errors = sbErrors.ToString();
        //    }
        //    //返回编译后的模板
        //    return codeTemplate;
        //}

        /// <summary>
        /// 去掉说明保留序号
        /// </summary>
        /// <param name="str"></param>
        /// <returns>序号集合</returns>
        protected static string ConvertToColName(string str)
        {
            StringBuilder sb = new StringBuilder();
            string[] a1 = str.Substring(0, str.Length - 1).Split(';');
            foreach (string a2 in a1)
            {
                string[] a3 = a2.Split('#');
                sb.Append(a3[1] + ";");
            }
            return sb.ToString().Substring(0, sb.ToString().Length - 1);
        }
    }
}